package zaphod.toy.simpleexample.internal;

import java.io.File;
import java.util.LinkedList;


public class FileFinder
{
    private final String path;
    private File[] result;


    public FileFinder( String path )
    {
        this.path = path;

    }


    public void run()
    {
        LinkedList<File> directories = new LinkedList<File>();
        LinkedList<File> result = new LinkedList<File>();

        File root = new File( path );
        directories.add( root );

        while ( !directories.isEmpty() )
        {
            File directory;
            directory = directories.pop();
            if ( !directory.isDirectory() )
            {
//                throw new RuntimeException( "" );
            }
            File[] children = directory.listFiles();
            for ( File child : children )
            {
                if ( child.isDirectory() )
                {
                    directories.push( child );
                }
                else
                {
                    result.push( child );
                }

            }

        }
        this.result = result.toArray( new File[result.size()] );
    }


    public File[] getResult()
    {
        return result;
    }

}
